{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting jieba\n",
      "  Downloading https://files.pythonhosted.org/packages/c6/cb/18eeb235f833b726522d7ebed54f2278ce28ba9438e3135ab0278d9792a2/jieba-0.42.1.tar.gz (19.2MB)\n",
      "Building wheels for collected packages: jieba\n",
      "  Running setup.py bdist_wheel for jieba: started\n",
      "  Running setup.py bdist_wheel for jieba: finished with status 'done'\n",
      "  Stored in directory: C:\\Users\\Administrator\\AppData\\Local\\pip\\Cache\\wheels\\af\\e4\\8e\\5fdd61a6b45032936b8f9ae2044ab33e61577950ce8e0dec29\n",
      "Successfully built jieba\n",
      "Installing collected packages: jieba\n",
      "Successfully installed jieba-0.42.1\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "twisted 18.7.0 requires PyHamcrest>=1.9.0, which is not installed.\n",
      "You are using pip version 10.0.1, however version 24.0 is available.\n",
      "You should consider upgrading via the 'python -m pip install --upgrade pip' command.\n"
     ]
    }
   ],
   "source": [
    "!pip install jieba"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Dumping model to file cache C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\jieba.cache\n",
      "Loading model cost 0.855 seconds.\n",
      "Prefix dict has been built successfully.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "精确模式: 研究生/在/研究/生命/的/起源\n",
      "全模式: 研究/研究生/生在/研究/研究生/生命/的/起源\n",
      "搜索引擎模式: 研究/研究生/研究/生命/的/起源\n"
     ]
    }
   ],
   "source": [
    "import jieba\n",
    "seg_list=jieba.cut(\"研究生在研究生命的起源\",cut_all=False)\n",
    "print(\"精确模式:\",\"/\".join(seg_list))\n",
    "seg_list=jieba.cut(\"研究生在研究生命的起源\",cut_all=True)\n",
    "print(\"全模式:\",\"/\".join(seg_list))\n",
    "seg_list=jieba.cut_for_search(\"研究生研究生命的起源\")\n",
    "print(\"搜索引擎模式:\",\"/\".join(seg_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "未删除停用词的前10个高频词:[('，', 59), ('的', 32), ('。', 22), ('、', 17), ('冬奥会', 16), ('\\n', 16), ('北京', 15), ('中国', 13), ('会徽', 12), ('年', 11)]\n",
      "删除停用词的前10个高频词:[('冬奥会', 16), ('北京', 15), ('中国', 13), ('会徽', 12), ('年', 11), ('冬', 8), ('2022', 7), ('运动员', 7), ('冰雪', 6), ('中心', 5)]\n"
     ]
    }
   ],
   "source": [
    "import jieba\n",
    "def TF_word(words,topk):\n",
    "    data={}\n",
    "    for k in words:\n",
    "        if k in data:\n",
    "            data[k]+=1\n",
    "        else:\n",
    "            data[k]=1\n",
    "    data=sorted(data.items(),key=lambda x:x[1],reverse=True)\n",
    "    return data[:topk]\n",
    "file_content=open('data/dongaohui.txt',encoding='utf-8').read()\n",
    "split_words=jieba.lcut(file_content)\n",
    "print(\"未删除停用词的前{0}个高频词:{1}\".format(10,TF_word(split_words,10)))\n",
    "stop_words=[line.strip()for line in open('data/stops_list.txt','r',encoding='utf-8').readlines()]\n",
    "stop_words.append('\\n')\n",
    "split_words2=[x for x in split_words if x not in stop_words]\n",
    "print(\"删除停用词的前{0}个高频词:{1}\".format(10,TF_word(split_words2,10)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "研究生/n研究/vn生命/vn的/uj起源/n!/x\n"
     ]
    }
   ],
   "source": [
    "import jieba.posseg as psg\n",
    "sent=\"研究生研究生命的起源!\"\n",
    "seg_list=psg.cut(sent)\n",
    "print(''.join(['{0}/{1}'.format(w,t) for w,t in seg_list]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
